package ioperation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements Ioperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入书名");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
//        pos既可以标记要被删除的书在书架的位置，又可以判断书籍是否存在
        int pos = -1;
        int useSize= bookList.getUseSize();
        for (int i = 0; i <useSize ; i++) {
            Book book=bookList.getBook(i);
            if (book.getName().equals(name)){
                pos=i;
                break;
            }
        }
        if (pos==-1){
            System.out.println("没有你要删除的书籍");
            return;
        }
        for (int j = pos; j <useSize-1 ; j++) {
//            定位到要被删除的书的后一本书
            Book book=bookList.getBook(j+1);
//            把这本书往要被删除的书的位置移动
            bookList.SetBooks(book,j);
        }
//        最后一个书架肯定是空的，要把它设置为null
        bookList.SetBooks(null,useSize);
//        实时更新书架上书籍数量2
        bookList.setUseSize(useSize-1);
        System.out.println("删除成功");
    }
}
